package priv.wangy.micro.common.po.valid.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import priv.wangy.micro.common.po.valid.IntRange;

import java.util.Objects;

/**
 * @author zhangjq
 * @since 2023-11-09
 */
public class IntRangeValidator implements ConstraintValidator<IntRange, Integer> {

    private int min;

    private int max;

    @Override
    public void initialize(IntRange constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        min = constraintAnnotation.min();
        max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return Objects.nonNull(value) && (value >= min) && (value <= max);
    }
}
